package models

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	_ "github.com/go-sql-driver/mysql"
	"github.com/microcosm-cc/bluemonday"

	//"time"
	"os"
)
//答案文本xss规则
var AnswerXssPolicy *bluemonday.Policy
func Init(){
	beego.Debug("model init")
	_ = orm.RegisterDriver("mysql", orm.DRMySQL)
	orm.Debug = true
	dbHost := os.Getenv("BEEGO_DBDSN")
	if dbHost == ""{
		dbHost = os.Getenv("PQ_MYSQL_USERNAME") + ":" + os.Getenv("PQ_MYSQL_PASSWORD")
		dbHost += "@(" + os.Getenv("PQ_MYSQL_HOST")+":" + os.Getenv("PQ_MYSQL_PORT") +")/" + os.Getenv("PQ_MYSQL_DB")
		dbHost += "?charset=utf8&&loc=Local"
 	}
	//root:root@(127.0.0.1:8889)/pqtest?charset=utf8&&loc=Local
	_ = orm.RegisterDataBase("default", "mysql", dbHost, 50, 600)
	_ = orm.RegisterDataBase("master", "mysql", dbHost, 50, 600)
	_ = orm.RegisterDataBase("slave", "mysql", dbHost, 50, 600)

	//orm.DefaultTimeLoc = time.UTC
	orm.RegisterModel(new (PqTestCategory),new (PqUser),new (PqTestPointCategory),new (PqCsTestLibrary),new (PqCsTestItem),new (PqTestItemAnswer),
		new (PqSlTestLibrary),new (PqSlTestItemMaterial),new (PqSlTestItem),new(PqCsSimuLibrary),new(PqCsSimuItemConnection),new(PqCsTestResource),
		new (PqStat),new (Download),new (Area))

	//xss protection
	AnswerXssPolicy = bluemonday.NewPolicy()
	AnswerXssPolicy.AllowStandardURLs()
	AnswerXssPolicy.AllowElements("img")
	AnswerXssPolicy.AllowAttrs("src").OnElements("img")
}